/*要求3.通过id删除学生信息
 * 要求4：删除完毕后，遍历所有的学生信息
 * 要求5：查询数组id为“heima002”的学生，如果存在则他的年龄+1岁*/
public class StudentTest1 {
    public static void main(String[] args) {
        //创建一个数组，存储学生对象
        Student[] students = new Student[3];
        //创建学生对象
        Student s1 = new Student(1,"张三",18);
        Student s2 = new Student(2,"李四",23);
        Student s3 = new Student(3,"王我",28);
        //添加学生对象到数组当中
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        //通过id删除学生信息
        int index = getindex(students,5);
        if(index >= 0){
            students[index] = null;
            System.out.println("删除成功");
            //遍历所有的学生信息
            for(int i = 0;i < students.length;i++){
                Student s = students[i];
                //判断学生对象是否为空
                if(s != null){
                    System.out.println(s.getId()+","+s.getName()+","+s.getAge());
                }
            }
        }else{
            System.out.println("id不存在");
        }

    }
    /*=========================方法区===========================*/
    //定以一个方法，通过id找到学生对象在数组中的索引
    public static int getindex(Student[] students,int id){
        //遍历数组，获取每一个学生对象的id
        for(int i = 0;i < students.length;i++){
            Student s = students[i];
            //找到id相同的学生对象，返回索引
            if(s != null){
                if(s.getId() == id){
                    return i;
                }
            }
        }return -1;
    }
}
